﻿namespace net.zxteam.zxlang.Lexer.ParserAssemblyGenerator.Internal
{
	using System;
	using System.IO;
	using System.Reflection;
	using System.Xml;
	using System.Xml.Xsl;

	internal class LocalXslTransform
	{
		private readonly System.Xml.Xsl.XslCompiledTransform _tranformer;

		public LocalXslTransform()
		{
			string classNamespace = this.GetType().Namespace;
			string className = this.GetType().Name;
			string fullXslResourceName = string.Concat(classNamespace, '.', className, ".xslt");

			using (Stream xslStream =
				Assembly.GetExecutingAssembly().GetManifestResourceStream(fullXslResourceName))
			{
				if (xslStream == null) throw new InvalidOperationException("Resource '" + fullXslResourceName + "' was not found.");
				using (XmlReader xslReader = XmlReader.Create(xslStream))
				{
					XslCompiledTransform tranformer = new XslCompiledTransform();
					tranformer.Load(xslReader);

					this._tranformer = tranformer;
				}
			}
		}

		public void Transform(XmlReader input, Stream output)
		{
			XsltArgumentList args = new XsltArgumentList();
			this._tranformer.Transform(input, args, output);
		}
	}
}
